package com.visionet.waveletData.web.service.plan;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.visionet.waveletData.web.common.utilities.props.PropsKeys;
import com.visionet.waveletData.web.common.utilities.props.PropsUtil;
import com.visionet.waveletData.web.common.utils.PageInfo;
import com.visionet.waveletData.web.entity.plan.PlanFile;
import com.visionet.waveletData.web.entity.plan.PlayPlan;
import com.visionet.waveletData.web.repository.plan.PlanFileDao;
import com.visionet.waveletData.web.repository.plan.PlayPlanDao;
import com.visionet.waveletData.web.repository.plan.specs.PlanFileSearchspecs;
import com.visionet.waveletData.web.service.BaseService;
import com.visionet.waveletData.web.vo.plan.PlanFileVo;

@Service
public class PlanFileService extends BaseService{
	
	@Resource
	private PlanFileDao planFileDao;
	
	@Resource
	private PlayPlanDao playPlanDao;

	@Autowired
	private PlayPlanService playPlanService;
	
	/**
	 * 根据id查询计划文件
	 * @param id
	 * @return
	 */
	public PlanFile findPlanFileById(Long id){
	      return planFileDao.findOne(id);
		}
	/**
	 * 根据条件查询播放计划
	 * @return
	 */
	public Page<PlanFile> findPlanFileByConditionForList(PlanFile planFile,PageInfo pageInfo){ 
		PageRequest pageRequest = null;
		if(pageInfo==null){
			pageInfo = new PageInfo();
			pageInfo.setPageSize(5000);
		}
		pageRequest=this.getPageRequestInfo(pageInfo);
		Page<PlanFile> page = planFileDao.findAll(PlanFileSearchspecs.searchTeamByCondition(planFile),pageRequest);
		pageInfo.setTotalCount(page.getNumberOfElements());
		return page;
		
	}
	public void deletePlanFile(Long id){
		PlanFile planFile = planFileDao.findOne(id);
		planFile.setIs_deleted("1");
		planFileDao.save(planFile); 
		PlayPlan playPlan = playPlanDao.findOne(Long.valueOf(planFile.getPlanId()));
		playPlan.setUpdateDate(new Date());
		playPlanDao.save(playPlan);
		if(playPlan != null){
			playPlanService.curdPlanData(playPlan.getId());
		}
	}
	public PageRequest getPageRequestInfo(PageInfo pageInfo) {
		Sort sort = new Sort(Direction.ASC, "id");
		return new PageRequest(pageInfo.getPageNumber() - 1,pageInfo.getPageSize(), sort);

	}
	
	public List<PlanFileVo> findPlanFileByPlanId(String planId){
		List<PlanFileVo> planFileVos = new ArrayList<PlanFileVo>();
		List<PlanFile> planFiles = planFileDao.findPlanFileByPlanId(planId);
		PlanFileVo planFileVo = null;
		if(null !=planFiles && planFiles.size()>0){
			for(int i=0;i<planFiles.size();i++){
				planFileVo = new PlanFileVo();
				PlanFile planFile = planFiles.get(i);
				BeanUtils.copyProperties(planFile, planFileVo);
				String filePath = PropsUtil.getProperty(PropsKeys.UPLOAD_FILE_PATH)+planFile.getFilePath()+"."+planFile.getFileType();
				planFileVo.setFilePath(filePath);
				planFileVo.setMfileSize(BigDecimal.valueOf(planFile.getFileSize()).divide(new BigDecimal("1024")).divide(new BigDecimal("1024")).setScale(2, BigDecimal.ROUND_HALF_UP)+"M");
				planFileVos.add(planFileVo);
			}		
		}
		return planFileVos;
	}
	
	public void savePlanFile(PlanFile planFile){
		planFileDao.save(planFile);
	}
	
	public List<PlanFile> findByFileNameAndPlanId(String fileName,String planId){
		return planFileDao.findPlanFileByFileNameAndPlanId(fileName, planId);
	}

}
